import { getFollowMaterialList, getFollowCaseList, getSupplierRecommendList, followSupplier } from '../../api/index'
import { formatTimeTwo } from '../../utils/util'

Component({
  properties: {
    topHeight: {
      type: Number,
      value: 0
    },
    onshowTime: {
      type: Number,
      value: 0
    }
  },
  data: {
    showSupplier: true,
    materailPage: 1,
    materailPageSize: 15,
    materailIsEmpty: false,
    materialList: [],
    supplierHeight: 0,
    tabsHeight: 0,
    activeTab: 'material',
    casePage: 1,
    casePageSize: 15,
    caseIsEmpty: false,
    caseList: [],
    supplierList: []
  },
  observers: {
    async onshowTime (val) {
      if (val) {
        await this.getMaterialList()
        await this.getCaseList()
        await this.getSupplierList()
        wx.stopPullDownRefresh()
      }
    }
  },
  methods: {
    async collectionSupplier ({ currentTarget: { dataset: { item, index } } }) {
      try {
        await followSupplier({ supplierId: item.id })
        wx.showToast({ title: '操作成功！', icon: 'none' })
        item.follow = !item.follow
        let key = `supplierList[${index}]`
        this.setData({ [key]: item })
        this.getMaterialList()
        this.getCaseList()
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    async cancelSupplier ({ currentTarget: { dataset: { item } } }) {
      try {
        await followSupplier({ supplierId: item.supplierId })
        wx.showToast({ title: '操作成功！', icon: 'none' })
        this.getMaterialList()
        this.getCaseList()
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    seeSupplier ({ currentTarget: { dataset: { item } } }) {
      wx.$nav.navigateTo(`/pages/supplier/index/index?id=${item.id}&name=${item.companyName}`)
    },
    seeMaterialDetail ({ currentTarget: { dataset: { item } } }) {
      wx.$nav.navigateTo(`/pages/materialDetail/index?id=${item.id || item.materialId}`)
    },
    hiddenSupplier () {
      this.setData({ showSupplier: false, supplierHeight: 0 })
    },
    changeTab ({ target: { dataset: { key } } }) {
      this.setData({ activeTab: key })
    },
    changeCase ({ detail }) {
      let index = this.data.caseList.findIndex(row => row.materialCaseId === detail.materialCaseId)
      if (index > -1) {
        let changeKey = `caseList[${index}]`
        this.setData({
          [changeKey]: detail
        })
        this.getMaterialList()
        this.getCaseList()
      }
    },
    async loadMaterialMore () {
      try {
        if (this.materialLoading || this.materialAlloaded) return
        this.materialLoading = true
        let query = {
          currentPage: this.data.materailPage,
          pageSize: this.data.materailPageSize,
        }
        const { ResultSet } = await getFollowMaterialList({ ...query })
        this.materialAlloaded = !ResultSet.userData.data.length
        this.data.materailIsEmpty = !ResultSet.userData.data.length
        this.setData({ materailIsEmpty: this.data.materailIsEmpty })
        if (ResultSet.userData.data.length) {
          ResultSet.userData.data.forEach(item => {
            item.createTime = formatTimeTwo(item.createTime, 'Y-D-M')
          })
          this.data.materailPage = this.data.materailPage + 1
          this.data.supplierList.push(...ResultSet.userData.data)
          this.setData({
            materialList: this.data.materialList,
            materailPage: this.data.materailPage
          })
        }
        this.materialLoading = false
      } catch (err) {
        this.materialLoading = false
        wx.showToast({ title: err.message || err.ResultSet.errors })
      }
    },
    async getMaterialList () {
      try {
        const { ResultSet } = await getFollowMaterialList({ currentPage: 1, pageSize: this.data.materailPageSize })
        this.data.materailIsEmpty = !ResultSet.userData.data.length
        if (ResultSet.userData.data.length) {
          this.data.materailPage = 2
          ResultSet.userData.data.forEach(item => {
            item.createTime = formatTimeTwo(item.createTime, 'Y-D-M')
          })
        }
        this.setData({
          materialList: ResultSet.userData.data,
          materailPage: this.data.materailPage,
          materailIsEmpty: this.data.materailIsEmpty
        })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    async loadCaseMore () {
      try {
        if (this.caseLoading || this.caseAlloaded) return
        this.caseLoading = true
        let query = {
          currentPage: this.data.casePage,
          pageSize: this.data.casePageSize,
        }
        const { ResultSet } = await getFollowCaseList({ ...query })
        this.caseAlloaded = !ResultSet.userData.data.length
        this.data.caseIsEmpty = !ResultSet.userData.data.length
        this.setData({ caseIsEmpty: this.data.caseIsEmpty })
        if (ResultSet.userData.data.length) {
          this.data.casePage = this.data.casePage + 1
          this.data.caseList.push(...ResultSet.userData.data)
          this.setData({
            caseList: this.data.caseList,
            casePage: this.data.casePage
          })
        }
        this.caseLoading = false
      } catch (err) {
        this.caseLoading = false
        wx.showToast({ title: err.message || err.ResultSet.errors })
      }
    },
    async getCaseList () {
      try {
        const { ResultSet } = await getFollowCaseList({ currentPage: 1, pageSize: this.data.casePageSize })
        this.data.caseIsEmpty = !ResultSet.userData.data.length
        if (ResultSet.userData.data.length) this.data.casePage = 2
        this.setData({
          caseList: ResultSet.userData.data,
          casePage: this.data.casePage,
          caseIsEmpty: this.data.caseIsEmpty
        })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    changeSupplier () {
      this.getSupplierList()
    },
    async getSupplierList () {
      try {
        const { ResultSet } = await getSupplierRecommendList()
        this.setData({ supplierList: ResultSet.userData.data })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    }
  },
  created () {
    this.getMaterialList()
    this.getCaseList()
    this.getSupplierList()
  },
  ready () {
    let query = this.createSelectorQuery();
    query.select('.supplier-list').boundingClientRect(rect=>{
      let height = rect.height
      this.setData({ supplierHeight: height })
    }).exec()
    query.select('.attention-tabs').boundingClientRect(rect=>{
      let height = rect.height
      this.setData({ tabsHeight: height })
    }).exec()
  }
})